﻿$(function() {
	/**
	 * 设备开动情况
	 * */
    var optbar = echarts.init(document.getElementById('optbar'));
    optionBar2 = {
    		tooltip : {
    	        trigger: 'axis',
    	        axisPointer: {
    		        type: 'shadow'
    		    }
    	    },
    	    grid: {
                top: '15%',
                left: '5%',
                right: '5%',
                bottom:'15%',
                containLabel: true
            },
            backgroundColor:'#e0e0e0',
            color:['#FF9966','#cc3f44'],
    	    legend: {
    	    	bottom:25,
    	        data:['超载时长(s)','最高超载比率(%)']
    	    },
    	    xAxis : [{
    	            type : 'category',
    	            data : [],
    	            splitLine:{
    	            	show:false
    	            },
		            axisLabel :{  
					    interval:0
					}
    	        }],
    	    yAxis : [{
    	    	    min:0,
    	            type : 'value',
    	            name : '超载时长(s)',
    	            axisLabel : {
    	                formatter: '{value}'
    	            },
    	            splitLine:{
    	            	show:false
    	            }
    	        }, {
    	        	min:0,
    	        	max:100,
    	            type : 'value',
    	            name : '最高超载比率(%)',
    	            axisLabel: {
    	            	formatter: '{value}%'
    	            },
    	            splitLine:{
    	            	show:false
    	            }
    	        }],
    	    series : [{
                name:'超载时长(s)',
                barMaxWidth:60,
                type:'bar',
                data:[]
            },{
                name:'最高超载比率(%)',
                yAxisIndex: 1,
                type:'line',
                data:[]
            }]
    	};
    optbar.setOption(optionBar2);
    
    /**
     * 初始化
     * */
    
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	});
    
    $('#cc').combobox({    
        onChange:function(value){
        	var getsliderTime=$('#ss').slider('getValue');
        	time=moment(getsliderTime).format("YYYY-MM-DD")+" "+value+":00:00";
        	getData();
        }
    }); 
    var lineId=0;
    var minute=$("#cc").datebox("getValue");
    $('#ss').slider('setValue',parseInt(moment().format("x")));
    var getsliderTime=$('#ss').slider('getValue');
	var	time=moment(getsliderTime).format("YYYY-MM-DD")+" "+minute+":00:00";
	getData();

    function getData(){
    	$.post(basePath+"/compare/overloadcompare/day.json",{"lineId":lineId,belongTime:time},function(res){
    		vm.$set('item',res);
//    		if(res.success){
			optbar.setOption({
    			xAxis: {
		            data: res.equNames
		        },
		        series : [{
	                name:'超载时长(s)',
	                data:res.overTimes
	            },{
	                name:'最高超载比率(%)',
	                data:res.overRates
	            }]
    		});
//    		}	
    	});
    }
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
       onComplete:function(value){//滑块值改变时触发
       	time=moment(value).format('YYYY-MM-DD')+" "+minute+":00:00";
       	getData();
       }
    });
    
    /**
     * 自定义时间按钮的点击事件
     * */
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
    
    /**
	  * 切换生产线的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onDblClick: function(node) {
			if (node.url) {
//				if(node.shifts){
					lineId=node.id;
					time=moment($('#ss').slider('getValue')).format("YYYY-MM-DD")+" "+minute+":00:00";
		    		getData();
					mePanel.dialog('close');
//				}else{
//					$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
//				}
			}else{
				$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
			}
		}
	});
  $("#switchEquipOK").click(function(){
  	var row = $("#linetree").tree('getSelected');
  	if(row.url){
//  		if(row.shifts){
  			lineId=row.id;
  			time=moment($('#ss').slider('getValue')).format("YYYY-MM-DD")+" "+minute+":00:00";
   			getData();
  			$('#switchEquip').dialog('close');
//  		}else{
//				$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
//			}
  	}else{
     		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择一条生产线!',timeout:3000});
     	}
  });
    
});